//
//  NotificationHelper.m
//  Sports-Go
//
//  Created by rimi on 16/9/28.
//  Copyright © 2016年 YRL. All rights reserved.
//

#import "NotificationHelper.h"

@implementation NotificationHelper
static NotificationHelper* instance;
+(NotificationHelper*) shareInstance
{
    static dispatch_once_t onceToken ;
    dispatch_once(&onceToken, ^{
        instance = [[[self class] alloc] init] ;
    });
    return instance ;
}
+ (void)deallocNoticationHelper{
    instance = nil;
}
-(void) registerLocalNotification:(UIApplication*)application
{
    application.applicationIconBadgeNumber = 0;//清除应用图标上的数字
    //关键：加上版本的控制
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_8_0
   
    UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
    UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
    if ([application respondsToSelector:@selector(registerUserNotificationSettings:)])
    {
        [[UIApplication sharedApplication] registerUserNotificationSettings:mySettings];
    }
#endif
}

-(void) addNotifiction:(NSString*) firedate keyA:(NSString*)key messageA:(NSString*)message
{
//    NSLog(@"addNotifiction");
    UILocalNotification *localNotification = [[UILocalNotification alloc] init];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"HH:mm:ss"];
    NSDate *now = [formatter dateFromString:firedate];//触发通知的时间
    //如果firedate传入的是XX:XX:XX格式在表示在固定的时间点发送通知，如果传入的是XX格式表示从现在开始XX秒后发送通知
    if(now == nil)
    {
        NSTimeInterval secs = [firedate doubleValue];
        now = [NSDate dateWithTimeIntervalSinceNow:secs];
    }
    
    localNotification.fireDate = now;
    //设置 时区
    localNotification.timeZone = [NSTimeZone defaultTimeZone];
    // 触发后，弹出警告框中显示的内容
    localNotification.alertBody = message;
    localNotification.alertAction = NSLocalizedString(@"View Details", nil);
    // 触发时的声音（这里选择的系统默认声音）
    localNotification.soundName = UILocalNotificationDefaultSoundName;
    // 触发频率（repeatInterval是一个枚举值，可以选择每分、每小时、每天、每年等）
    localNotification.repeatInterval = kCFCalendarUnitHour;//测试用暂时写死为每隔一天 0：不重复
    // 需要在App icon上显示的未读通知数（设置为1时，多个通知未读，系统会自动加1，如果不需要显示未读数，这里可以设置0）
    localNotification.applicationIconBadgeNumber = 0;
    // 设置通知的id，可用于通知移除，也可以传递其他值，当通知触发时可以获取
    localNotification.userInfo = @{@"id":key};
    // 注册本地通知
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
 
}
- (void)removeLocalNotificationByKey:(NSString*)key {
    // 取出全部本地通知
    NSArray *notifications = [UIApplication sharedApplication].scheduledLocalNotifications;
    // 设置要移除的通知id
    NSString *notificationId = key;
    // 遍历进行移除
    for (UILocalNotification *localNotification in notifications) {
        // 将每个通知的id取出来进行对比
        if ([[localNotification.userInfo objectForKey:@"id"] isEqualToString:notificationId]) {
            // 移除某一个通知
            [[UIApplication sharedApplication] cancelLocalNotification:localNotification];
        }
    }
}
- (void)removeLocalAllNotification {
    
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
    
}

@end
